package com.hx.reporter.service.impl;

import com.hx.common.utils.DateUtils;
import com.hx.reporter.domain.Group;
import com.hx.reporter.mapper.GroupMapper;
import com.hx.reporter.mapper.GroupUserMapper;
import com.hx.reporter.service.IGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 群组Service业务层处理
 * 
 * @author ${author}
 * @date 2023-05-28
 */
@Service
public class GroupServiceImpl implements IGroupService {

    @Autowired
    private GroupMapper groupMapper;

    @Autowired
    private GroupUserMapper groupUserMapper;

    /**
     * 查询群组
     *
     * @param groupId 群组主键
     * @return 群组
     */
    @Override
    public Group selectGroupByGroupId(String groupId)
    {
        return groupMapper.selectGroupByGroupId(groupId);
    }


    @Override
    public List<Group> selectGroupListByUserId(String userId) {
        List<String> groupIds = groupUserMapper.selectGroupIdsByUserId(userId);

        List<Group> groupList = new ArrayList<>();
        for (String groupId : groupIds) {
            groupList.add(groupMapper.selectGroupByGroupId(groupId));
        }
        return groupList;
    }

    /**
     * 查询群组列表
     * 
     * @param group 群组
     * @return 群组
     */
    @Override
    public List<Group> selectGroupList(Group group)
    {
        return groupMapper.selectGroupList(group);
    }

    /**
     * 新增群组
     * 
     * @param group 群组
     * @return 结果
     */
    @Override
    public int insertGroup(Group group)
    {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        group.setGroupId(uuid);
        return groupMapper.insertGroup(group);
    }

    /**
     * 修改群组
     * 
     * @param group 群组
     * @return 结果
     */
    @Override
    public int updateGroup(Group group)
    {
        group.setUpdateTime(DateUtils.getNowDate());
        return groupMapper.updateGroup(group);
    }

    /**
     * 批量删除群组
     * 
     * @param groupIds 需要删除的群组主键
     * @return 结果
     */
    @Override
    public int deleteGroupByGroupIds(String[] groupIds)
    {
        return groupMapper.deleteGroupByGroupIds(groupIds);
    }

    /**
     * 删除群组信息
     * 
     * @param groupId 群组主键
     * @return 结果
     */
    @Override
    public int deleteGroupByGroupId(String groupId)
    {
        return groupMapper.deleteGroupByGroupId(groupId);
    }
}
